﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TweetSharp.Twitter.Extensions;
using TweetSharp.Twitter.Fluent;
using TweetSharp.Twitter.Model;

namespace Halfwit.Pollers
{
    public class MentionsPoller : TwitterPoller
    {
        public MentionsPoller(Action<TwitterPoller, TwitterResult> callback)
            : base(callback, () => Properties.Settings.Default.MentionsRefreshInterval)
        {
            Description = "Mentions";
        }

        protected override ITwitterLeafNode CreateRequest(IFluentTwitter twitter)
        {
            var request = twitter.Statuses().Mentions();
            if (LastId.HasValue)
            {
                request = request.Since(LastId.Value);
            }
            else
            {
                request = request.Take(Properties.Settings.Default.MaxTweets);
            }
            return request;
        }

        protected override void OnSuccessfulRequest(TwitterResult result)
        {
            var tweets = result.AsStatuses();
            if (tweets == null || tweets.Count() == 0) return;

            LastId = tweets.Max(t => t.Id);
        }
    }
}
